package huanghui.netstorage.util;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 日志工具
*
 */
public class LogUtil
{
	
	private static final String TAG = "NetStorage"; 
	private static final boolean isDug = true;
	private static String[] arr = {"LogUtil"};
	private static final int INFO = 1;
	private static final int DEBUG = INFO + 1;
	private static final int ERROR = DEBUG + 1;
	private static final int WARN = ERROR + 1;
	

	
	/**
	 * Log.i 打印日志信息
	 * @param o 类文	 
	 * @param msg 打印的日志信内容	 
	 */
	public static void i(Object o,String msg)
	{
		showInfo(INFO, msg, o);
	}
	
	public static void i(Class<? extends Object> c,String msg)
	{
		showInfo(INFO, msg, c);
	}
	
	/**
	 * Log.d打印日志信息
	 * @param c 类文�?	 * @param msg 打印的日志信�?	 */
	public static void d(Object o,String msg)
	{
		showInfo(DEBUG, msg, o);
	}
	
	public static void d(Class<? extends Object> c,String msg)
	{
		showInfo(DEBUG, msg, c);	
	}
	
	public static void e(Object o,String msg)
	{
		showInfo(ERROR, msg, o);
	}
	
	public static void e(Class<? extends Object> c,String msg)
	{
		showInfo(ERROR, msg, c);
	}
	
	public static void w(Object o,String msg)
	{
		showInfo(WARN, msg, o);
	}
	
	public static void w(Class<? extends Object> c,String msg)
	{

		showInfo(WARN, msg, c);
	}
	
	/**
	 * 获取文件类名（去除之前的包名)
	 * @param c
	 * @return 类名
	 */
	private static String getClassName(Object o){
		if (o == null)
		{
			return "";
		}
		Class<? extends Object> c = o.getClass();
		return getClassName(c);
	}
	
	private static String getClassName(Class<? extends Object> c)
	{
		if (c == null)
		{
			return "";
		}
		
		String cName = c.getName();
		String[] tmp = cName.split("\\.");
		cName = tmp[tmp.length-1];
		
		return cName;
	}
	
	
	private static boolean compareName(String name)
	{
		
		for (String n:arr)
		{
			if (n.equals(name))
			{
				return true;
			}
		}
		return false;
	}
	
	private static void showInfo(int type, String msg, Object o)
	{
		
		if (!isDug)
		{
			return;
		}
		String cName = getClassName(o);
		msg = "["+cName + "] " +msg;
		if (compareName(cName))
		{
			return;
		}
		
		printLog(type, msg);
	}
	
	private static void showInfo(int type, String msg, Class<? extends Object> c)
	{
		if (!isDug)
		{
			return;
		}
		String cName = getClassName(c);
		msg = cName + " " +msg;
		if (compareName(cName))
		{
			return;
		}

		printLog(type, msg);
	}	
	
	
	private static String getCurrentTime()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
		return sdf.format(new Date());
	}
	
	
	private static String getLeval(int type)
	{
		String result = "";
		switch (type)
		{
		case INFO:
			result = "INFO";
			break;
		case DEBUG:
			result = "DEBUG";
			break;
		case ERROR:
			result = "ERROR";
			break;
		case WARN:
			result = "WARN";
			break;

		}
		return result;

	}
	
	private static void printLog(int type, String msg)
	{
		String log = getCurrentTime()+ "["+TAG+"]"+"["+getLeval(type)+"]"+msg;
		if (type == ERROR)
		{
			System.err.println(log);
		}
		else
		{
			System.out.println(log);
		}
		
	}
	
	
}
